package cn.tedu.submarine;
import sun.applet.Main;

import java.awt.Graphics;
import java.util.Random;
import javax.swing.ImageIcon;
//海洋对象
public abstract class SeaObject {
    public static final int LIVE = 0;//活着
    public static final int DEAD = 1;//死了
    protected int state = LIVE;//当前状态(默认活着)


    protected int width; //宽
    protected int height;//高
    protected int x;     //x坐标
    protected int y;     //y坐标
    protected int speed; //移动速度
    //专门给侦查潜艇,水雷地雷潜艇
    //三个潜艇的x/y/speed都一样,所以数据可以写死,不需要传参
    //因为三个潜艇的width height不一样,所以数据可以写死,需要传参
    public SeaObject(int width,int height){
        this.width = width;
        this.height = height;
        x = -width;
        y = new Random().nextInt(World.HEIGHT-height-150+1)+150;
        speed = new Random().nextInt(3)+1;
    }

    //给战舰水雷地雷炸弹提供的
    public SeaObject(int width,int height,int x,int y,int speed){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }
    //移动
    public abstract void move();

    /**获取对象的图片*/
    public abstract ImageIcon getImage();

    /**判断是否活着*/
    public boolean isLive(){
        return state==LIVE;//若当前状态为LIVE,表示活着,返回true,否则false
    }

    /**判断是否死着*/
    public boolean isDEAD(){
        return  state==DEAD;//若当前状态为DEAD,表示为死了,返回true,否则false
    }

    /**画图片 g:画笔*/
    public void paintImage(Graphics g){
        if (isLive()){//若活着
            this.getImage().paintIcon(null,g,this.x,this.y);
        }
    }

    /** 生成雷对象------发射鱼雷或水雷*/
    public SeaObject shootThunder(){
        int x = this.x+this.width;//x:潜艇的x+潜艇的宽
        int y = this.y-5;         //y:潜艇的y-固定的5
        if (this instanceof TorpedoSubmarine){//若潜艇为鱼雷潜艇,则发射鱼雷
            return new Torpedo(x,y);
        }else if (this instanceof  MineSubmarine){//若潜艇为水雷潜艇,则发射水雷
            return new Mine(x,y);
        }else {//若潜艇为侦查潜艇,则不发雷
            return null;
        }


    }

    /**检测潜艇是否越界*/
    public boolean isOutOfBounds(){
        return x>=World.WIDTH;//如果潜艇的x>=窗口的宽,即为越界了
    }

    /**检测碰撞*/
    public boolean isHit(SeaObject other){
        int x1 = this.x-other.width; //x1:潜艇的x-炸弹的宽
        int x2 = this.x+this.width;  //x2:潜艇的x+潜艇的宽
        int y1 = this.y-other.height;//y1:潜艇的y-炸弹的高
        int y2 = this.y+this.height; //y2:潜艇的y+炸弹的高
        int x = other.x;             //x:炸弹的x
        int y = other.y;             //y:炸弹的y

        return x>=x1 && x<=x2 &&
               y>=y1 && y<=y2;//x在x1与x2之间,并且,y在y1和y2之间
    }

    /**海洋对象去死*/
    public  void geDead(){
        state = DEAD; //将对象的状态修改为DEAD死了的
    }
}
